//
//  YBMineActivationCodeTableHeader.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/7/1.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBMineActivationCodeTableHeader: UIView {

    /// ActivationCod
    var comfigActivationCodeBlock: ((String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubviews([titleLabel, kTextField, activeBtn, activeLabel])
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(22)
            make.height.equalTo(18)
        }
        
        kTextField.snp.makeConstraints { make in
            make.height.equalTo(50)
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
        }
        
        activeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(kScreenWidth*0.7, 44))
            make.centerX.equalToSuperview()
            make.top.equalTo(kTextField.snp.bottom).offset(offset40)
        }
        
        activeLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(activeBtn.snp.bottom).offset(offset80)
            make.height.equalTo(22)
            make.bottom.equalTo(-10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var titleLabel = self.bk_addLabel(text: "输入激活码兑换", font: .systemFont(ofSize: 15, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var kTextField: UITextField = {
        let tf = UITextField()
        tf.borderStyle = .none
        tf.bk_addCornerBorder(radius: 4, borderWidth: 0.5, borderColor: ColorDef.gray184)
        tf.bk_addBlankLeftView(width: 16)
        tf.bk_addPlaceholder("请输入激活码激活")
        tf.backgroundColor = .lightWhiteDark27
        tf.textColor = .lightBlack51DarkLight230
        tf.textAlignment = .left
        tf.font = .systemFont(ofSize: 15)
        tf.clearButtonMode = .whileEditing
        return tf
    }()
    
    private lazy var activeBtn: UIButton = {
        let btn = self.bk_addGradientButton(title: "立即激活", font: .systemFont(ofSize: 16, weight: .medium), titleColor: .white, gradientSize: CGSizeMake(kScreenWidth*0.7, 44), colors: [kRGBColor(88, 182, 255), kRGBColor(40, 128, 237)])
        btn.bk_addStyleWith(cornerRadius: 22, corners: .allCorners)
        btn.bk_addTarget { [unowned self] sender in
            // 收起键盘
            self.kTextField.resignFirstResponder()
            
            guard let codeString = self.kTextField.text, codeString.isValid else {
                HintTool.toast("请输入兑换码")
                return
            }
            comfigActivationCodeBlock?(codeString)
        }
        return btn
    }()
    
    private lazy var activeLabel = self.bk_addLabel(text: "激活记录", font: .systemFont(ofSize: 18, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230, align: .center)
    
    public func refreshActiveLabel(by count: Int) {
        activeLabel.isHidden = count > 0
    }
}
